package la.iok.hzsvn.lewin.movie.controller;

import la.iok.hzsvn.lewin.movie.core.Token;
import la.iok.hzsvn.lewin.movie.model.ClueCardParam;
import la.iok.hzsvn.lewin.movie.model.ClueCardVo;
import la.iok.hzsvn.lewin.movie.model.DigestInfo;
import la.iok.hzsvn.lewin.movie.model.ObsSignedUrl;
import la.iok.hzsvn.lewin.movie.service.ClueCardService;
import la.iok.hzsvn.share.response.ListDataResponse;
import la.iok.hzsvn.share.response.OperatorResponse;
import la.iok.hzsvn.share.response.OperatorResponses;
import la.iok.hzsvn.share.response.SingleDataResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("clueCards")
public class ClueCardController {
    private final ClueCardService clueCardService;

    public ClueCardController(ClueCardService clueCardService) {
        this.clueCardService = clueCardService;
    }

    @PostMapping("group/{id}")
    public SingleDataResponse<Long> save(@RequestHeader Token token,@PathVariable Long id, @RequestBody ClueCardParam param){
        return new SingleDataResponse<>(clueCardService.save(token,id,param));
    }
    @PutMapping("{id}")
    public OperatorResponse update(@RequestHeader Token token,@PathVariable Long id, @RequestBody ClueCardParam param){
        clueCardService.update(token,id,param);
        return OperatorResponses.success();
    }
    @DeleteMapping("{id}")
    public OperatorResponse delete(@RequestHeader Token token,@PathVariable Long id){
        clueCardService.delete(token,id);
        return OperatorResponses.success();
    }
    @GetMapping("movie/{id}")
    public ListDataResponse<ClueCardVo> movieClueCards(@RequestHeader Token token,@PathVariable Long id){
        return new ListDataResponse<>(clueCardService.movieClueCards(token,id));
    }

    @PostMapping("{id}/hdFile")
    public OperatorResponse uploadHdFile(@RequestHeader Token token, @PathVariable Long id, MultipartFile file, String path){
        clueCardService.uploadHdFile(token,id,path,file);
        return OperatorResponses.success();
    }

    @PostMapping("{id}/sdFile")
    public OperatorResponse uploadSdFile(@RequestHeader Token token, @PathVariable Long id, MultipartFile file, String path){
        clueCardService.uploadSdFile(token,id,path,file);
        return OperatorResponses.success();
    }

    @GetMapping("{id}/hdDigest")
    public SingleDataResponse<String> hdDigest(@PathVariable Long id){
        String digest = clueCardService.hdFileDigest(id);
        if(digest != null){
            SingleDataResponse<String> res = new SingleDataResponse<>();
            res.setData(digest);
            return res;
        }
        return new SingleDataResponse<>();
    }

    @PutMapping("{id}/hdDigest")
    public SingleDataResponse<ObsSignedUrl> hdFileDigest(@RequestHeader Token token, @PathVariable Long id, @RequestBody DigestInfo digest){
        return new SingleDataResponse<>(clueCardService.uploadHdFile(token,id,digest));
    }

    @GetMapping("{id}/sdDigest")
    public SingleDataResponse<String> sdDigest(@PathVariable Long id){
        String digest = clueCardService.sdFileDigest(id);
        if(digest != null){
            SingleDataResponse<String> res = new SingleDataResponse<>();
            res.setData(digest);
            return res;
        }
        return new SingleDataResponse<>();
    }

    @PutMapping("{id}/sdDigest")
    public SingleDataResponse<ObsSignedUrl> sdFileDigest(@RequestHeader Token token, @PathVariable Long id, @RequestBody DigestInfo digest){
        return new SingleDataResponse<>(clueCardService.uploadSdFile(token,id,digest));
    }

    @GetMapping("{id}/hdStorePath")
    public SingleDataResponse<String> hdStorePath(@PathVariable Long id){
        String storePath = clueCardService.hdFileStorePath(id);
        return OperatorResponses.stringSingleDataResponse(storePath);
    }

    @GetMapping("{id}/sdStorePath")
    public SingleDataResponse<String> sdStorePath(@PathVariable Long id){
        String storePath = clueCardService.sdFileStorePath(id);
        return OperatorResponses.stringSingleDataResponse(storePath);
    }

    @GetMapping("{id}/sdFile")
    public void downloadSdFile(@RequestHeader Token token, HttpServletResponse response, @PathVariable Long id){
        clueCardService.downloadSdFile(token,response,id);
    }

    @GetMapping("{id}/hdFile")
    public void downloadHdFile(@RequestHeader Token token, HttpServletResponse response, @PathVariable Long id){
        clueCardService.downloadHdFile(token,response,id);
    }
}
